var layer;
var education_type;
var marriageType;
var housing;
var housingTime;
var insuranceId;
var childNum;

$(function() {
	layui.use('layer', function() {
		layer = layui.layer;
	});
	
	getUserInfo();
})

function getUserInfo() {
	$.ajax({
		type: "get",
		url: "http://localhost:8810/loan/app/user/userInfo",
		dataType: "json",
		//这里是重点
		beforeSend: setHeader,
		success: function(data) {
			var userEntity = data.data.user;
			console.log(data.data.user)
			$("#userName").val(userEntity.userName);
			$("#cmnd").val(userEntity.cmnd);
			$("#birthday").val(userEntity.birthday.substring(0,10));
			$("#facebookLink").val(userEntity.facebookLink);
			$("#zaloLink").val(userEntity.zaloLink);
			if(userEntity.sex == 0){
				 $('input:radio:first').attr('checked', 'checked');
			}else{
				 $('input:radio:last').attr('checked', 'checked');
			}
			
			education_type = userEntity.education;
			marriageType = userEntity.marriageStatus;
			housing = userEntity.housing;
			housingTime = userEntity.housingTime;
			insuranceId = userEntity.insuranceId;
			childNum = userEntity.childNum;
			
			getInfoType("education_type");
			
			getInfoType("marriage");
			
			getInfoType("children_num");
			
			getInfoType("house");
			
			getInfoType("residence");
			
			getInfoType("insurance");
			
		},
		error: function(error) {
			layer.msg(error.message);
		}
	})
}

function getInfoType(type) {
	$.ajax({
		type: "get",
		url: "http://localhost:8810/loan/app/dict/getList",
		data: {
			type: type
		},
		dataType: "json",
		//这里是重点
		beforeSend: setHeader,
		success: function(data) {
			if (type == 'education_type') {
				if (data.code == 0) {
					$("#ordersType").empty();
					$("#ordersType").append('<option value="" class="scvbn"></option>');
					$.each(data.list, function(index, item) {
						if (education_type == item.dictValue) {
							$("#ordersType").append('<option value="' + item.dictValue + '" selected="selected" class="scvbn">' + item.dictLabel +
								' </option>');
						} else {
							$("#ordersType").append('<option value="' + item.dictValue + '" class="scvbn">' + item.dictLabel +
								' </option>');
						}
					})
				}
			} else if (type == 'marriage') {
				$("#marriages").empty();
				$("#marriages").append('<option value="" class="scvbn"></option>');
				$.each(data.list, function(index, item) {
					if (marriageType == item.dictValue) {
						$("#marriages").append('<option value="' + item.dictValue + '" selected="selected" class="scvbn">' + item.dictLabel +
							' </option>');
					} else {
						$("#marriages").append('<option value="' + item.dictValue + '" class="scvbn">' + item.dictLabel +
							' </option>');
					}
				})
			} else if (type == 'children_num') {
				$("childNum").empty();
				$("#childNum").append('<option value="" class="scvbn"></option>');
				$.each(data.list, function(index, item) {
					if (childNum == item.dictValue) {
						$("#childNum").append('<option value="' + item.dictValue + '" selected="selected" class="scvbn">' + item.dictLabel +
							' </option>');
					} else {
						$("#childNum").append('<option value="' + item.dictValue + '" class="scvbn">' + item.dictLabel +
							' </option>');
					}
				})
			} else if (type == 'house') {
				$("house").empty();
				$("#house").append('<option value="" class="scvbn"></option>');
				$.each(data.list, function(index, item) {
					if (housing == item.dictValue) {
						$("#house").append('<option value="' + item.dictValue + '" selected="selected" class="scvbn">' + item.dictLabel +
							' </option>');
					} else {
						$("#house").append('<option value="' + item.dictValue + '" class="scvbn">' + item.dictLabel +
							' </option>');
					}
				})
			} else if (type == 'residence') {
				$("residence").empty();
				$("#residence").append('<option value="" class="scvbn"></option>');
				$.each(data.list, function(index, item) {
					if (housing == item.dictValue) {
						$("#residence").append('<option value="' + item.dictValue + '" selected="selected" class="scvbn">' + item.dictLabel +
							' </option>');
					} else {
						$("#residence").append('<option value="' + item.dictValue + '" class="scvbn">' + item.dictLabel +
							' </option>');
					}
				})
			} else if (type == 'insurance') {
				$("insurance").empty();
				$("#insurance").append('<option value="" class="scvbn"></option>');
				$.each(data.list, function(index, item) {
					if (insuranceId == item.dictValue) {
						$("#insurance").append('<option value="' + item.dictValue + '" selected="selected" class="scvbn">' + item.dictLabel +
							' </option>');
					} else {
						$("#insurance").append('<option value="' + item.dictValue + '" class="scvbn">' + item.dictLabel +
							' </option>');
					}
				})
			}
		},
		error: function(error) {
			layer.msg(error.message);
		}
	})
}

function submit() {
	var userName = $("#userName").val();
	var cmnd = $("#cmnd").val();
	var birthday = $("#birthday").val();
	var sex = $("input[name='sex']:checked").val();
	var ordersType = $("#ordersType").val();
	var marriages = $("#marriages").val();
	var childNum = $("#childNum").val();
	var house = $("#house").val();
	var residence = $("#residence").val();
	var insurance = $("#insurance").val();
	var fackbookLink = $("#facebookLink").val();
	var zaloLink = $("#zaloLink").val();
	if ($.trim(userName) == '') {
		layer.msg("Nhập tên đầy đủ is null");
		return false;
	}
	if ($.trim(birthday) == '') {
		layer.msg("Ngày sinh is null");
		return false;
	}
	if ($.trim(ordersType) == '') {
		layer.msg("Vui lòng chọn trình độ học vấn is null");
		return false;
	}
	if ($.trim(marriages) == '') {
		layer.msg("Tình trạng hôn nhân is null");
		return false;
	}
	if ($.trim(childNum) == '') {
		layer.msg("Vui lòng chọn số con cái is null");
		return false;
	}
	if ($.trim(house) == '') {
		layer.msg("Tình trạng cư trú is null");
		return false;
	}
	if ($.trim(residence) == '') {
		layer.msg("Vui lòng chọn thời gian cư trú is null");
		return false;
	}
	if ($.trim(insurance) == '') {
		layer.msg("Vui lòng chọn loại bảo hiểm bạn đang sử dụng is null");
		return false;
	}

	var param = {
		userName: userName,
		cmnd: cmnd,
		sex: sex,
		education: ordersType,
		marriageStatus: marriages,
		childNum: childNum,
		housing: house,
		housingTime: residence,
		insuranceId: insurance,
		facebookLink: fackbookLink,
		zaloLink: zaloLink,
		birthday: birthday
	}
	$.ajax({
		url: 'http://localhost:8810/loan/app/apply/saveOne',
		type: 'post',
		contentType: 'application/json',
		data: JSON.stringify(param),
		beforeSend: setHeader,
		success: function(data) {
			if (data.code == 0) {
				window.location.href = "grzx2.html";
			} else {
				layer.msg(data.msg);
			}
		}
	});
}
